<?php

namespace app\zyhsapi\lib;

use app\common\cachemodel\zlcr3\Zlcr3ConsultAccount;
use app\common\cachemodel\ZlcrAstr;
use app\common\cachemodel\ZlcrAstrAccount;
use app\common\cachemodel\ZlcrAstrMessage;
use app\common\lib\Alidayu;
use app\common\lib\Util;
use app\zyhsapi\model\ZyhsAstrTasks;
use app\zyhsapi\model\ZyhsMessageCenter;
use app\zyhsapi\model\ZyhsOrders;
use app\zyhsapi\model\ZyhsUser;
use app\zyhsapi\model\ZyhsUserAddQuestion;
use app\zyhsapi\model\ZyhsUserQuestion;
use app\zyhsapi\model\ZyhsUserQuestionComment;
use think\Log;
use think\queue\Job;

class ZyhsQuestionAnswerJob
{
    /**
     * 问答接单检测 如果未接单则退款 8小时后自动退款未接单
     * @param Job $job
     * @param $jobData
     */
    public function AstrReceivingCheck(Job $job, $jobData)
    {
        // $jobData = [
        //     'qid' =>咨询问题ID
        // ];
        $jobAttempts = $job->attempts();
        if ($jobAttempts > 2)
        {   // 失败两次 则删除
            $job->delete();
            return;
        }
        try
        {
            $nowTime = time();
            $questionModel = new ZyhsUserQuestion();
            $questionInfo = $questionModel->getInfoById($jobData['qid'], ['status', 'discard_num', 'consult_num', 'receiving_num', 'order_num', 'uid', 'extend', 'refund_amount', 'good_amount']);
            if ($questionInfo['status'] === 1 && $questionInfo['receiving_num'] < $questionInfo['consult_num'])
            {   // 问题 无人接单    或者  问题被弃答过 且此时 无人接单
                $extend = $questionInfo['extend'];
                if (isset($extend['is_receiving_refund']) && $extend['is_receiving_refund'])
                {   // 已退款
                    $job->delete();
                    return;
                }
                if ($questionInfo['discard_num'] >= 3)
                {
                    $reason = '无法回答自动取消';
                } else
                {
                    $reason = '暂无咨询师接单自动取消';
                }
                $status = $questionInfo['receiving_num'] === 0 ? 3 : 2;    // 解答中
                $res = $questionModel->refund($jobData['qid'], $reason);
                if ($res['status'])
                {
                    $extend['is_receiving_refund'] = 1; // 接单已退款
                    $refundData = $extend['refund_data'] ?? [];
                    $refundData[] = [
                        'type' => 1,    // 类型 1=>8小时未接单退款 2=>申述退款
                        'reason' => $reason,
                        'refund_amount' => $res['refund_amount'] * 10,   // 退款金额 单位 准币
                        'time' => $nowTime,
                    ];
                    $extend['refund_data'] = $refundData;
                    $refundAmount = $questionInfo['refund_amount'] + ($res['refund_amount'] * 10);
                    $refundAmount = min($refundAmount, $questionInfo['good_amount']);
                    $questionModel->UpdateData($jobData['qid'], [
                        'status' => $status, //
                        'refund_amount' => $refundAmount,   // 单位 准币
                        'extend' => $extend,
                        'is_read' => 0,
                        'uid' => $questionInfo['uid'],
                    ]);

                    if ($questionInfo['discard_num'] >= 3)
                    {   // 弃答次数大于3
                        if ($questionInfo['receiving_num'] === 0)
                        {   // 未被 占星师 抢单 过
                            $msgContent = "你开启的悬赏问答，订单编号：{$questionInfo['order_num']}因问题无法回答自动取消，资金将原路返还，请注意查收。请检查问题后重新下单。";
                            $smsTpid = 'SMS_464400301';
                            $tpContent = ['order_num' => $questionInfo['order_num']];
                        } else
                        {   // 被 占星师 抢单 过
                            $msgContent = "你开启的悬赏问答，订单编号：{$questionInfo['order_num']}因存在咨询师选择问题不合理弃答的情况，发生部分退款，资金将原路返还，请注意查收。请检查问题后重新下单。";
                            $smsTpid = 'SMS_464330323';
                            $tpContent = ['order_num' => $questionInfo['order_num']];
                        }
                    } else
                    {   // 发消息
                        if ($questionInfo['receiving_num'] === 0)
                        {   // 未被 占星师 抢单 过
                            $msgContent = "你开启的悬赏问答，服务编号：{$questionInfo['order_num']}因暂无咨询师接单自动取消，资金将原路返还，请注意查收。你可以重新下单。";
                            $smsTpid = 'SMS_464315287';
                            $tpContent = ['order_num' => $questionInfo['order_num']];
                        } else
                        {   // 被 占星师 抢单 过
                            $msgContent = "你开启的悬赏问答，订单编号：{$questionInfo['order_num']}因存在咨询师未接单的情况，发生部分退款，资金将原路返还，请注意查收。";
                            $smsTpid = 'SMS_464320348';
                            $tpContent = ['order_num' => $questionInfo['order_num']];
                        }
                    }
                    // 消息中心添加信息
                    $messageData = [
                        'status' => 0,
                        'is_read' => 0,
                        'range' => $questionInfo['uid'],
                        'type' => 2,
                        'reid' => 0,
                        'cid' => 4,//
                        'title' => '订单取消通知',
                        'content' => $msgContent,// 消息中心跳转地址
                        'link' => "question_detail?qid={$jobData['qid']}&app_type=2&need_auth=0",  // 订单详情
                        'target' => 'local',
                        'create_time' => $nowTime,
                    ];
                    (new ZyhsMessageCenter())->pushMessage($questionInfo['uid'], $messageData);
                    $pushData = ['action' => 'extend', 'target' => 'local', 'link' => "question_detail?qid={$jobData['qid']}&need_auth=0"];
                    // PUSH推送报告首页
                    (new ZyhsPush())->pushUserMsg($questionInfo['uid'], $msgContent, $pushData);
                    // 发送短信
                    $userInfo = (new ZyhsUser())->getInfoById($questionInfo['uid']);
                    if ($userInfo && $userInfo['tel'])
                    {   // 发送 短信通知 用户
                        Alidayu::getInstance()->zyhsMsgSmsSend(86, $userInfo['tel'], $smsTpid, $tpContent);
                    }
                } else
                {
                    Log::write('准有好事 - 问答接单检测 如果未接单则退款 QuestionAnswerJob->AstrReceivingCheck  问答问题【' . $jobData['qid'] . '】退款失败:' . $res['msg'], 'error', true);
                }
            }
        }
        catch (\Exception $e)
        {
            $function = __FUNCTION__;
            Log::write("准有好事 - 问答队列 异常QuestionAnswerJob->{$function}(),JobData = " . json_encode($jobData, JSON_UNESCAPED_UNICODE) . "：错误消息：{$e->getMessage()};FILE:{$e->getFile()};Line:{$e->getLine()}", 'error');
        }
        // 已被接单 ｜ 已取消
        $job->delete();
    }

    /**
     * 占星师 15分钟内还未回复的用户进行通知 检测
     * @param Job $job
     * @param $jobData
     * @throws \Exception
     */
    public function AstrReplyNotify(Job $job, $jobData)
    {
        // $jobData = [
        //     'task_id' =>任务ID
        // ];
        $jobAttempts = $job->attempts();
        if ($jobAttempts > 2)
        {   // 失败两次 则删除
            $job->delete();
            return;
        }

        try
        {
            $astrTaskModel = new ZyhsAstrTasks();
            $taskInfo = $astrTaskModel->getInfoById($jobData['task_id']);
            if ($taskInfo['status'] === 1)  // 状态  1=>待解答【已接单】 2=>已弃答 3=>已解答【可追问48小时后自动完成】 4=>已解答【追问回复超时，不可追问48小时后自动完成】 5=>追问待解答 6=>已完成  7=>答案回复超时 8=>已完成且收益扣除【用户申述已取消】 9=>已完成且收益扣除【追问回复超时未完成】
            {   // 进行中
                // 如果15分钟了 占星师还未回复 发送 提醒消息
                // 发送短信
                $tel = (new ZlcrAstr())->getInfoById($taskInfo['astr_id'], ['tel']);
                Util::sendHySms([$tel['tel']], '', 131490, ['task_id' => $jobData['task_id'], 'time' => '15分钟']);
            }
        }
        catch (\Exception $e)
        {
            $function = __FUNCTION__;
            Log::write("准有好事 - 问答队列 异常QuestionAnswerJob->{$function}(),JobData = " . json_encode($jobData, JSON_UNESCAPED_UNICODE) . "：错误消息：{$e->getMessage()};FILE:{$e->getFile()};Line:{$e->getLine()}", 'error');
        }

        $job->delete();
    }

    /**
     * 占星师 30分钟内还未回复的用户进行取消问题 检测
     * @param Job $job
     * @param $jobData
     * @throws \Exception
     */
    public function QuestionAstrReplyCheck(Job $job, $jobData)
    {
        // $jobData = [
        //     'task_id' =>任务ID
        // ];
        $jobAttempts = $job->attempts();
        if ($jobAttempts > 2)
        {   // 失败两次 则删除
            $job->delete();
            return;
        }
        try
        {
            $nowTime = time();
            $astrTaskModel = new ZyhsAstrTasks();
            $zlcr3ConsultAccountModel = new Zlcr3ConsultAccount();
            $taskInfo = $astrTaskModel->getInfoById($jobData['task_id']);
            if ($taskInfo['status'] === 1)  // 未回复
            {   // 进行中
                $questionModel = new ZyhsUserQuestion();
                $questionInfo = $questionModel->getInfoById($taskInfo['qid'], ['discard_num', 'consult_num', 'receiving_num', 'extend', 'uid']);
                $extend = $questionInfo['extend'];
                $receivingData = [];
                foreach ($questionInfo['extend']['receiving_tasks'] as $receivingDatum)
                {
                    if ($receivingDatum['task_id'] !== $jobData['task_id'])
                    {
                        $receivingData[] = $receivingDatum;
                    }
                }
                $extend['receiving_tasks'] = $receivingData;
                // 如果60分钟了 占星师还未回复 取消任务
                $questionModel->UpdateData($taskInfo['qid'], [
                    'discard_num' => $questionInfo['discard_num'] + 1,
                    'receiving_num' => count($receivingData),
                    'extend' => $extend,    // 弃答
                    'status' => 1,  //
                    'uid' => $questionInfo['uid'],
                ]);
                // 获取近30天内 已经回复超时 次数
                $monthBeforTime = strtotime("-1 month");
                $timeOutNum = $astrTaskModel->where(['astr_id' => $taskInfo['astr_id'], 'status' => 7, 'create_time' => ['>', $monthBeforTime]])->count();
                // 更新占星师任务状态信息
                $taskUpdate = [
                    'status' => 7,  //  状态  1=>待解答【已接单】 2=>已弃答 3=>已解答【可追问48小时后自动完成】 4=>已解答【追问回复超时，不可追问48小时后自动完成】 5=>追问待解答 6=>已完成  7=>答案回复超时 8=>已完成且收益扣除【用户申述已取消】 9=>已完成且收益扣除【追问回复超时未完成】
                    'update_time' => $nowTime,
                ];
                // 任务回复超时 扣分
                $isPause = false;
                $pauseTime = 0;
                if ($timeOutNum >= 3)
                {   // 超时次数 已经有3次或以上了
                    $isPause = true;
                    $taskUpdate['is_lock_receiving'] = 1;
                    $pauseTime = 2147483647;   // int32最大值
                }
                // 扣分 或 扣分 并 冻结接单资格
                $astrConsultInfo = $zlcr3ConsultAccountModel->getInfoById($taskInfo['astr_id']);
                $zlcr3ConsultAccountModel->changeAstrLevelScore($astrConsultInfo, $jobData['task_id'], 2, -20, 3, $pauseTime, $pauseTime > 0 ? '30日内问答回复超时第4次' : '');
                // 更新占星师任务状态信息
                $astrTaskModel->UpdateData($jobData['task_id'], $taskInfo['astr_id'], $taskUpdate);

                $msgContent = $isPause ? "你的任务{$jobData['task_id']}因回复超时被取消，已扣除相应的成长值，因30天内4次回复超时，账号准有好事问答接单功能被封禁。"
                    : "你的任务{$jobData['task_id']}因回复超时被取消，已扣除相应的成长值，将会影响你的等级和收益，30天内4次回复超时账号将封禁接单功能。";
                // 占星师站内信
                (new ZlcrAstrMessage())->add($taskInfo['astr_id'], [
                    'status' => 0,
                    'is_read' => 0,
                    'astr_id' => $taskInfo['astr_id'],
                    'type' => 9, // 默认消息
                    'title' => '任务取消通知',
                    'content' => $msgContent,
                    'target' => 'local',
                    'link' => '/quiz/answer?task_id=' . $jobData['task_id'] . '&app_type=2',
                    'create_time' => $nowTime,
                ]);
            }
        }
        catch (\Exception $e)
        {
            $function = __FUNCTION__;
            Log::write("准有好事 - 问答队列 异常QuestionAnswerJob->{$function}(),JobData = " . json_encode($jobData, JSON_UNESCAPED_UNICODE) . "：错误消息：{$e->getMessage()};FILE:{$e->getFile()};Line:{$e->getLine()}", 'error');
        }
        $job->delete();
    }

    /**
     * 任务完成 检测 占星师 回答48小时后
     * @param Job $job
     * @param $jobData
     * @throws \Exception
     */
    public function QuestionSuccess(Job $job, $jobData)
    {
        // $jobData = [
        //     'task_id' =>任务ID
        // ];
        $jobAttempts = $job->attempts();
        if ($jobAttempts > 2)
        {   // 失败两次 则删除
            $job->delete();
            return;
        }
        try
        {
            $nowTime = time();
            $astrTaskModel = new ZyhsAstrTasks();
            $orderModel = new ZyhsOrders();
            $questionModel = new ZyhsUserQuestion();
            $zlcr3ConsultAccountModel = new Zlcr3ConsultAccount();
            $taskInfo = $astrTaskModel->getInfoById($jobData['task_id']);
            $questionInfo = $questionModel->getInfoById($taskInfo['qid']);
            if (in_array($taskInfo['status'], [3, 4], true))
            {   // 已解答 待追问状态 仅 该状态进行完成操作 其他状态不处理 如在追问中，则追问回答的时候进行完成逻辑处理｜追问被取消时处理
                // 更新已接单 任务列表
                $receivingTasks = $questionInfo['extend']['receiving_tasks'];
                $status = 3;
                foreach ($receivingTasks as $k => $receivingTaskItem)
                {
                    if ($jobData['task_id'] === (int)$receivingTaskItem['task_id'])
                    {   // 是否已回复
                        $receivingTasks[$k]['is_success'] = 1;  // 已完成
                    } elseif (!isset($receivingTaskItem['is_success']))
                    {
                        $status = 2;
                    }
                }
                $questionInfo['extend']['receiving_tasks'] = $receivingTasks;
                // 问答状态更新完成
                $questionModel->UpdateData($taskInfo['qid'], [
                    'status' => $status, // 状态 1=>接单中 2=>解答中 3=>已完成
                    'extend' => $questionInfo['extend'],
                    'update_time' => $nowTime,
                    'uid' => $questionInfo['uid'],
                ]);
                // 订单状态 更新已完成
                if ($status === 3)  // 已完成
                {   // 问题 完成
                    $orderDataUpdateState = $orderModel->UpdateData($questionInfo['order_num'], ['status' => 4]);
                    if ($orderDataUpdateState === false)
                    {
                        Log::write('准有好事 - 任务完成更新问答订单状态失败 任务ID:' . $jobData['task_id'], 'error', true);
                    }
                }
                $addQuestionLists = (new ZyhsUserAddQuestion())->getQuestionAddQuestionInfo($taskInfo['id']);
                // 追问 完成
                foreach ($addQuestionLists as $addQuestionItem)
                {
                    if ($addQuestionItem['order_num'] && $addQuestionItem['status'] === 4)
                    {
                        $addUpdateOrder = $orderModel->UpdateData($addQuestionItem['order_num'], ['status' => 4]);
                        if ($addUpdateOrder === false)
                        {
                            Log::write('准有好事 - 任务完成更新追问订单状态失败 任务ID:' . $jobData['task_id'], 'error', true);
                        }
                    }
                }
                // 计算本单收益  $questionInfo ['astr_id','task_id','good_amount',‘consult_num’,'gid']
                $taskEarning = $questionModel->getQuestionEarning([
                    'astr_id' => $taskInfo['astr_id'],
                    'task_id' => $jobData['task_id'],
                    'good_amount' => $questionInfo['good_amount'],
                    'consult_num' => $questionInfo['consult_num'],
                    'gid' => $questionInfo['gid'],
                ]);
                // 占星师任务完成
                $astrTaskModel->UpdateData($jobData['task_id'], $taskInfo['astr_id'], [
                    'status' => $taskInfo['status'] === 3 ? 6 : 9,  // 状态  1=>待解答【已接单】 2=>已弃答 3=>已解答【可追问48小时后自动完成】 4=>已解答【追问回复超时，不可追问48小时后自动完成】 5=>追问待解答 6=>已完成  7=>答案回复超时 8=>已完成且收益扣除【用户申述已取消】 9=>已完成且收益扣除【追问回复超时未完成】
                    'earning' => $taskEarning['earning'],
                    'update_time' => $nowTime,
                    'success_time' => $nowTime,
                ]);
                if ($taskInfo['status'] === 3)
                {   // 占星师任务状态正常
                    // 计算占星师收益 并 更新占星师收益
                    $proportionData = $zlcr3ConsultAccountModel->getQuestionAnswerPercent();
                    $astrInfo = (new ZlcrAstr())->getInfoById($taskInfo['astr_id']);
                    $teamId = $astrInfo['team_id'] ?? 0;
                    (new ZlcrAstrAccount())->astrZyhsQuestionAnswerEarningsSend([
                        'earning' => $taskEarning['earning'],
                        'total_amount' => $taskEarning['total_amount'],
                        'astr_id' => $taskInfo['astr_id'],
                        'type' => 1,
                        'gid' => $questionInfo['gid'],
                        'proportion' => $proportionData['question'],  // 获取分成比例
                        'question_type' => $questionInfo['question_type'],
                        'task_id' => $jobData['task_id'],
                        'team_id' => $teamId,
                    ]);
                }
                if ($taskInfo['is_comment'])
                {   // 用户已经评价过了
                    $commentInfo = (new ZyhsUserQuestionComment())->getInfoById($taskInfo['id']);
                    if ($commentInfo['level'] !== 3)
                    {
                        $commentLevelChangeScoreData = [
                            1 => -20,
                            2 => -10,
                            3 => 0,
                            4 => 3,
                            5 => 5,
                        ];
                        $consultAccountInfo = $zlcr3ConsultAccountModel->getInfoById($taskInfo['astr_id']);
                        // 更新能力分
                        $zlcr3ConsultAccountModel->changeAstrLevelScore($consultAccountInfo, $jobData['task_id'], 2, $commentLevelChangeScoreData[$commentInfo['level']], 1);
                    }
                } else
                {
                    // 如果没有评价 则发送 72小时候自动评价 队列
                    Util::queuePush('zyhs_question_answer_question_comment_check', ['task_id' => $jobData['task_id']], 259200);
                }
            }
        }
        catch (\Exception $e)
        {
            $function = __FUNCTION__;
            Log::write("准有好事 - 问答队列 异常QuestionAnswerJob->{$function}(),JobData = " . json_encode($jobData, JSON_UNESCAPED_UNICODE) . "：错误消息：{$e->getMessage()};FILE:{$e->getFile()};Line:{$e->getLine()}", 'error');
        }
        $job->delete();
    }

    /**
     * 问答 问题 评价检测 未评价 自动评价 完成后3天后
     * @param Job $job
     * @param $jobData
     * @throws \Exception
     */
    public function QuestionCommentCheck(Job $job, $jobData)
    {
        // $jobData = [
        //     'task_id' =>任务ID
        // ];
        $jobAttempts = $job->attempts();
        if ($jobAttempts > 2)
        {   // 失败两次 则删除
            $job->delete();
            return;
        }
        try
        {
            $zyhsAstrTasksModel = new ZyhsAstrTasks();
            $taskInfo = $zyhsAstrTasksModel->getInfoById($jobData['task_id'], ['is_comment', 'astr_id', 'uid']);

            if (!$taskInfo['is_comment'])
            {   // 没有评价 自动评价
                // 写入评价数据
                (new ZyhsUserQuestionComment())->save([
                    'uid' => $taskInfo['uid'],
                    'task_id' => $jobData['task_id'],
                    'level' => 3,
                    'tids' => '178',
                    'comment' => '',
                    'astr_id' => $taskInfo['astr_id'],
                    'comment_type' => 2,
                    'create_time' => time(),
                ]);
                // 更新评价状态
                $zyhsAstrTasksModel->UpdateData($jobData['task_id'], $taskInfo['astr_id'], [
                    'is_comment' => 1,
                ]);
            }
        }
        catch (\Exception $e)
        {
            $function = __FUNCTION__;
            Log::write("准有好事 - 问答队列 异常QuestionAnswerJob->{$function}(),JobData = " . json_encode($jobData, JSON_UNESCAPED_UNICODE) . "：错误消息：{$e->getMessage()};FILE:{$e->getFile()};Line:{$e->getLine()}", 'error');
        }
        $job->delete();
    }

    /**
     * 追问回复超时检测  3小时内未回复 自动退款
     * @param Job $job
     * @param $jobData
     * @throws \Exception
     */
    public function AddQuestionReplyTimeoutCheck(Job $job, $jobData)
    {
        // $jobData = [
        //     'add_question_id' =>追问ID
        // ];
        $jobAttempts = $job->attempts();
        if ($jobAttempts > 2)
        {   // 失败两次 则删除
            $job->delete();
            return;
        }
        // 追问回复超时 付费  48小时内    免费 不检测
        /**
         * 1、48小时内
         * 用户 追问退款
         * 任务进行中，但不能追问了
         * 占星师 任务失败 结算
         * 2、超48小时
         * 用户退款
         * 任务自动完成
         * 占星师 任务失败 结算
         */
        try
        {
            $questionModel = new ZyhsUserQuestion();
            $addQuestionModel = new ZyhsUserAddQuestion();
            $addQuestionInfo = $addQuestionModel->getInfoById($jobData['add_question_id']);
            $nowTime = time();
            $astrTaskModel = new ZyhsAstrTasks();
            if ($addQuestionInfo['status'] === 2)
            {
                $taskInfo = $astrTaskModel->getInfoById($addQuestionInfo['task_id']);
                if (empty($addQuestionInfo['order_num']))
                {   // 免费 想占星师 发送超时提醒
                    // 占星师站内信
                    (new ZlcrAstrMessage())->add($taskInfo['astr_id'], [
                        'status' => 0,
                        'is_read' => 0,
                        'astr_id' => $taskInfo['astr_id'],
                        'type' => 9, // 默认消息
                        'title' => '任务异常通知',
                        'content' => "你的任务{$taskInfo['id']}追问回复已经超时，请尽快回复，官方介入后将扣除本任务收益。",
                        'target' => 'local',
                        'link' => '/quiz/answer?task_id=' . $taskInfo['id'] . '&app_type=2',
                        'create_time' => $nowTime,
                    ]);
                    // 发送短信 提醒  【准了】你的任务{task_id}有新的追问未回复，请尽快回复追问，回复超时将冻结任务收益
                    $tel = (new ZlcrAstr())->getInfoById($taskInfo['astr_id'], ['tel']);
                    Util::sendHySms([$tel['tel']], '', 53582, ['task_id' => $taskInfo['id']]);
                } else
                {
                    // 付费 并且还是待解答状态 追问退款
                    $res = $addQuestionModel->refund($jobData['add_question_id'], '咨询师追问回复超时');
                    if ($res['status'])
                    {   // 追问状态关闭
                        $addQuestionModel->UpdateData($jobData['add_question_id'], [
                            'status' => 6,  // 状态 1=>待支付  2=>待解答 3=>未支付已关闭 4=>已完成 5=>占星师申述已取消  6=>追问回复超时已取消  7=>用户申述通过退款
                        ]);
                        // 发送消息
                        $messageData = [
                            'status' => 0,
                            'is_read' => 0,
                            'range' => $taskInfo['uid'],
                            'type' => 2,
                            'reid' => 0,
                            'cid' => 4,//
                            'title' => '问答追问取消通知',
                            'content' => '你开启的悬赏问答追问，订单编号：' . $addQuestionInfo['order_num'] . '因占星师超时未回复取消，资金将原路返还，请注意查收。',// 消息中心跳转地址
                            'link' => "question_detail?qid={$taskInfo['qid']}&need_auth=0",
                            'target' => 'local',
                            'create_time' => $nowTime,
                        ];
                        (new ZyhsMessageCenter())->pushMessage($taskInfo['uid'], $messageData);
                        $pushData = ['action' => 'extend', 'target' => 'local', 'link' => "question_detail?qid={$taskInfo['qid']}&need_auth=0"];
                        // PUSH推送报告首页
                        (new ZyhsPush())->pushUserMsg($taskInfo['uid'], '你开启的悬赏问答追问，订单编号：' . $addQuestionInfo['order_num'] . '因占星师超时未回复取消，资金将原路返还，请注意查收。', $pushData);
                        // 发送短信
                        $userInfo = (new ZyhsUser())->getInfoById($taskInfo['uid']);
                        if ($userInfo && $userInfo['tel'])
                        {   // 发送 短信通知 用户
                            Alidayu::getInstance()->zyhsMsgSmsSend(86, $userInfo['tel'], 'SMS_464305291', ['order_num' => $addQuestionInfo['order_num']]);
                        }
                        // 获取近30天内 已经回复超时 次数
                        $monthBeforTime = strtotime("-1 month");
                        $timeOutNum = $astrTaskModel->where(['astr_id' => $taskInfo['astr_id'], 'status' => ['in', [4, 9]], 'create_time' => ['>', $monthBeforTime]])->count();

                        // 计算本单收益
                        $questionInfo = $questionModel->getInfoById($taskInfo['qid']);
                        $taskEarning = $questionModel->getQuestionEarning([
                            'astr_id' => $taskInfo['astr_id'],
                            'task_id' => $addQuestionInfo['task_id'],
                            'good_amount' => $questionInfo['good_amount'],
                            'consult_num' => $questionInfo['consult_num'],
                            'gid' => $questionInfo['gid'],
                        ]);
                        // 任务关闭数据
                        $taskUpdate = [
                            'status' => 4,
                            'earning' => $taskEarning['earning'],
                            'deduct_earning_time' => $nowTime, // 扣除收益时间
                            'lock_reason' => '咨询师追问回复超时',
                            'update_time' => $nowTime,
                        ];
                        // 获取超时次数 或扣除额外能力分 或冻结账号
                        $isPause = false;
                        $pauseTime = 0;
                        if ($timeOutNum >= 3)
                        {
                            $isPause = true;
                            $taskUpdate['is_lock_receiving'] = 1;
                            $pauseTime = 2147483647;   // int32最大值
                        }
                        // 扣分 或 扣分 并 冻结接单资格
                        $zlcr3ConsultAccountModel = new Zlcr3ConsultAccount();
                        $astrConsultInfo = $zlcr3ConsultAccountModel->getInfoById($taskInfo['astr_id']);
                        $zlcr3ConsultAccountModel->changeAstrLevelScore($astrConsultInfo, $addQuestionInfo['task_id'], 2, -10, 4, $pauseTime, $pauseTime > 0 ? '30日内问答追问超时第4次' : '');
                        // 任务状态关闭
                        $astrTaskModel->UpdateData($addQuestionInfo['task_id'], $taskInfo['astr_id'], $taskUpdate);
                        // 发送 占星师站内信
                        $msgContent = $isPause ? "你的任务{$addQuestionInfo['task_id']}追问回复超时，因30日内4次追问回复超时，准有好事问答接单资格冻结，已经扣除你本任务的全部收益。如有疑问请联系官方。"
                            : "因你的任务{$addQuestionInfo['task_id']}追问回复超时，已经扣除你本任务的全部收益，并做扣除能力分处理。如有疑问请联系官方。";

                        // 占星师站内信
                        (new ZlcrAstrMessage())->add($taskInfo['astr_id'], [
                            'status' => 0,
                            'is_read' => 0,
                            'astr_id' => $taskInfo['astr_id'],
                            'type' => 9, // 默认消息
                            'title' => '任务异常通知',
                            'content' => $msgContent,
                            'target' => 'local',
                            'link' => '/quiz/answer?task_id=' . $taskInfo['id'] . '&app_type=2',
                            'create_time' => $nowTime,
                        ]);

                        // 如果咨询时间 距离现在超过48小时了 则结算任务 添加结算队列 延迟5秒
                        if ($nowTime > $taskInfo['reply_time'] + 172800)
                        {   // 任务完成
                            Util::queuePush('zyhs_question_answer_question_success_check', ['task_id' => $taskInfo['id']], 5);
                        }
                    }
                }
            }
        }
        catch (\Exception $e)
        {
            $function = __FUNCTION__;
            Log::write("准有好事 - 问答队列 异常QuestionAnswerJob->{$function}(),JobData = " . json_encode($jobData, JSON_UNESCAPED_UNICODE) . "：错误消息：{$e->getMessage()};FILE:{$e->getFile()};Line:{$e->getLine()}", 'error');
        }
        $job->delete();
    }

    /**
     * 白天 追问回复检测 未回复发送消息提醒  -- 咱不发送
     * @param Job $job
     * @param $jobData
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function AddQuestionReplyNotifyCheck(Job $job, $jobData)
    {
        // $jobData = [
        //     'add_question_id' =>追问ID
        //     'astr_id' =>占星师ID
        // ];
        $jobAttempts = $job->attempts();
        if ($jobAttempts > 2)
        {   // 失败两次 则删除
            $job->delete();
            return;
        }
        try
        {
            $addQuestionModel = new ZyhsUserAddQuestion();
            $addQuestionInfo = $addQuestionModel->getInfoById($jobData['add_question_id']);
            if ($addQuestionInfo['status'] === 2)   // 状态 1=>待支付  2=>待解答 3=>未支付已关闭 4=>已完成 5=>占星师申述已取消  6=>追问回复超时已取消  7=>用户申述通过退款
            {   // 发送短信 提醒  【准有好事】你的任务{task_id}有新的追问未回复，请尽快回复追问，回复超时将冻结任务收益
                $tel = (new ZlcrAstr())->getInfoById($jobData['astr_id'], ['tel']);
                // Util::sendHySms([$tel['tel']], '', 62664, ['task_id' => $addQuestionInfo['task_id']]);
            }
        }
        catch (\Exception $e)
        {
            $function = __FUNCTION__;
            Log::write("准有好事 - 问答队列 异常QuestionAnswerJob->{$function}(),JobData = " . json_encode($jobData, JSON_UNESCAPED_UNICODE) . "：错误消息：{$e->getMessage()};FILE:{$e->getFile()};Line:{$e->getLine()}", 'error');
        }
        $job->delete();
    }
}
